home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Tennis / PatternBot.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-30  |  2.4 KB  |  68 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    PatternBot.cpp
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // Include files
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #include "stdafx.h"
  14. #include "patternbot.h"
  15.  
  16. //----------------------------------------------------------------------------------------------
  17. // SetPatternSize(): Sets the size of the pattern that is generated
  18. //----------------------------------------------------------------------------------------------
  19.  
  20. void CPatternBot::SetPatternSize(int nSize) {
  21.     m_Pattern.SetSize(nSize);
  22.  
  23.     for (int i = 0; i<m_Pattern.GetSize(); i++) {
  24.         int Destination;
  25.  
  26.         // randomly chooce a destination to play the ball to
  27.         RandomPredictor.GetPrediction(Destination);
  28.  
  29.         // calculate location
  30.         m_Pattern[i] = field.left + OUTBOX_WIDTH + (Destination * (field.right-field.left-2*OUTBOX_WIDTH)) / PADDLE_ANGLES;
  31.     }
  32.  
  33.     // reset positition in the pattern
  34.     m_PatternPos = 0;
  35.  
  36.     // enable targeting
  37.     bTargeting = TRUE;
  38.  
  39.     // set first target
  40.     OnBallCollision();
  41. }
  42.  
  43. //----------------------------------------------------------------------------------------------
  44. // OnBallCollision(): Determine next ball destination (to maintain the pattern)
  45. //----------------------------------------------------------------------------------------------
  46.  
  47. void CPatternBot::OnBallCollision() {
  48.     BallDestination.x = m_Pattern[m_PatternPos];
  49.     BallDestination.y = (int)py + direction * (field.bottom-field.top);
  50.  
  51.     m_PatternPos++;
  52.     if (m_PatternPos >= m_Pattern.GetSize()) {
  53.         m_PatternPos = 0;
  54.     }
  55. }
  56.  
  57. //----------------------------------------------------------------------------------------------
  58. // ResetForService(): resets the pattern bot for the service, and generates a new pattern
  59. //----------------------------------------------------------------------------------------------
  60.  
  61. void CPatternBot::ResetForService(BOOL HasService) {
  62.     // call default parent implementation
  63.     CAiBot::ResetForService(HasService);
  64.  
  65.     // generate a new pattern
  66.     SetPatternSize(m_Pattern.GetSize());
  67. }
  68.